-
Notifications
You must be signed in to change notification settings - Fork 1
/
centrality.py
162 lines (144 loc) · 4.99 KB
/
centrality.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import pyTigerGraphBeta as tg
import pandas as pd
import numpy as np
import streamlit as st
import pydeck as pdk
import json
import plotly.express as px
from util import *
def set_map(data, color):
data = set_score(data)
st.pydeck_chart(pdk.Deck(
map_style="mapbox://styles/mapbox/light-v9",
initial_view_state=pdk.data_utils.compute_view(data[['lng','lat']]),
tooltip={"text": "{Vertex_ID}\n{name}"},
layers=[
pdk.Layer(
"ScatterplotLayer",
data=data,
pickable=True,
opacity=0.6,
stroked=True,
filled=True,
radius_scale=6,
get_radius=["score"],
radius_pixels=["score"],
radius_min_pixels=3,
radius_max_pixels=200,
# line_width_min_pixels=1,
get_position=['lng', 'lat'],
get_fill_color=color,
),
]
))
def degreeCentrality(conn, country):
if (country == "None"):
return
params = "country=" + country
# conn.debug = True
res = conn.runInstalledQuery('dc_by_country', params=params, timeout=16000)
res = pd.DataFrame(res[0]["@@topScores"])
return res
def ClosenessCentrality(conn, country, maxHops):
if (country == "None"):
return
# maxHops = st.slider('Max hops', 1, 10, 3)
params = "country=" + country + "&" + "maxHops=" + str(maxHops) + "&" + "display=" + str(False)
res = conn.runInstalledQuery('cc_by_country', params=params, timeout=16000)
res = res[0]["@@topScores"]
res = pd.DataFrame(res)
return res
def betweennessCentrality(conn, country, maxHops):
if (country == "None"):
return
# maxHops = st.slider('Max hops', 1, 10, 3)
params = "country=" + country + "&" + "maxHops=" + str(maxHops)
res = conn.runInstalledQuery('betweenness_cent', params=params, timeout=16000)
res = res[0]['Start']
data = pd.DataFrame([], columns=['Vertex_ID', 'name', 'lat', 'lng', 'score'])
for i in range(len(res)):
id = res[i]['attributes']['Start.id']
name = res[i]['attributes']['Start.name']
lat = res[i]['attributes']['Start.latitude']
lng = res[i]['attributes']['Start.longitude']
score = res[i]['attributes']['Start.@cent']
data.loc[i] = [id, name, lat, lng, score]
return data
def PageRank(conn, country):
if (country == "None"):
return
# maxHops = st.slider('Max hops', 1, 10, 3)
params = "country=" + country
res = conn.runInstalledQuery('pageRank_by_country', params=params, timeout=16000)
res = pd.DataFrame(res[0]['@@topScores'])
return res
def set_degreeCentrality_map(conn, data, maxHops):
if (data == "None"):
return
res = degreeCentrality(conn, data)
set_map(res, color_dic.get('dc'))
def set_closenessCentrality_map(conn, data, maxHops):
if (data == "None"):
return
res = ClosenessCentrality(conn, data, maxHops)
set_map(res, color_dic.get('cc'))
def set_betweennessCentrality_map(conn, data, maxHops):
if (data == "None"):
return
res = betweennessCentrality(conn, data, maxHops)
set_map(res, color_dic.get('bc'))
def set_pageRank_map(conn, data, maxHops):
if (data == "None"):
return
res = PageRank(conn, data, maxHops)
set_map(res, color_dic.get('pr'))
def overall_chart(bc, cc, dc, pr):
if len(dc) > 30:
dc = dc.head(30)
cc = cc.head(30)
bc = bc.head(30)
pr = pr.head(30)
source = pd.DataFrame([], columns=['name', 'score', 'centrality'])
idx = 0
source, idx = combine_data(dc, idx, source, "degree centrality")
source, idx = combine_data(cc, idx, source, "closeness centrality")
source, idx = combine_data(bc, idx, source, "betweennes centrality")
source, idx = combine_data(pr, idx, source, "page rank")
with st.beta_expander('See raw data'):
st.dataframe(source)
st.write("")
st.write('')
st.altair_chart(set_chart(source))
def getCountryCentrality(conn, data, maxHops):
if (data == "None"):
return
#get the centrality data
dc = degreeCentrality(conn, data)
cc = ClosenessCentrality(conn, data, maxHops)
bc = betweennessCentrality(conn, data, maxHops)
pr = PageRank(conn, data)
col1, col2 = st.beta_columns(2)
col3, col4 = st.beta_columns(2)
with col1:
st.write('Degree Centrality')
set_map(dc, color_dic.get('dc'))
with col2:
st.write('Closeness Centrality')
set_map(cc, color_dic.get('cc'))
with col3:
st.write('Betweenness Centrality')
set_map(bc, color_dic.get('bc'))
with col4:
st.write('Page Rank')
set_map(pr, color_dic.get('pr'))
print(bc)
print(cc)
print(dc)
print(pr)
st.write("")
st.write("")
if len(dc) > 30:
st.subheader("Centrality Values Overview(top 30)")
else:
st.subheader("Centrality Values Overview")
overall_chart(bc, cc, dc, pr)